package com.WISSC.serviceImpl;

import com.WISSC.bean.*;
import com.WISSC.bean.layuiBean.ResultPage;
import com.WISSC.bean.layuiBean.VoPage;
import com.WISSC.mapper.UserMapper;
import com.WISSC.service.*;
import com.WISSC.util.Respons.ResponseUtil;
import com.WISSC.util.date.DateUtil;
import com.WISSC.util.exception.CommonException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 管理员用户 服务实现类
 * </p>
 *
 * @author 冯政尚
 * @since 2019-05-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    MenuService menuService;

    @Autowired
    RoleService roleService;

    @Autowired
    RoleMenuService roleMenuService;

    @Autowired
    RoleUserService roleUserService;

    /**
     * @param voPage
     * @param voPage
     * @return 分页
     */
    @Override
    public ResultPage<User> selectMyPage(VoPage voPage, String username) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("username", username);
        List<User> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<User> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }


    /**
     * @param user
     * @param session
     * @return 登陆
     */
    @Override
    public boolean login(User user, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", user.getUsername());
        map.put("password", user.getPassword());
        List<User> users = baseMapper.login(map);
        //如果用户存在，放入session域
        if (users.size() > 0) {
            boolean flag = users.get(0).getPassword().equals(user.getPassword());
            if(flag){
                System.out.println("1:" + users.get(0));
                System.out.println("2:" + users.get(0).getRoles());
                users.get(0).getRoles().setMenus(menuService.findMenuByRole(users.get(0).getRoles()));
                session.setAttribute("user", users.get(0));
                System.err.println("Impl层:" + users.get(0).toString());
                return true;
            }else{
                System.out.println("密码错误---用户输入密码:"+user.getPassword());
                return false;
            }

        } else {
            return false;
        }
    }


    //1.查询所有信息
    public List<User> getAll() {

        User user = new User();
        List list = baseMapper.selectByExampleWithDept(user);
        System.out.println("list:" + user);
        return list;


    }

    /**
     * @param username
     * @return 搜索
     */
    @Override
    public List<User> sousuo(String username) {
        System.out.println("Impl:" + username);
        Map map = new HashMap();
        map.put("username", username);
        System.out.println("Impl:" + map);
        List list = baseMapper.sousuo(map);
        System.out.println("Impl:" + list);
        return list;
    }

    @Override
    public Map<String, Object> saveUser(String username, String password,Integer status, String menuId, Long id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("username", username);
        map.put("id", id);
        if (this.listByMap(map).size() > 0) {
            throw new CommonException("该用户已存在");
        }
        System.out.println(status);
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setStatus(status);
        user.setCreateTime(DateUtil.date());
        //添加user
        if (this.save(user)) {
            //添加role
            Role role = new Role();
            role.setUserId(user.getId());
            role.setRoleValue(UUID.randomUUID() + "");
            if (roleService.save(role)) {
                RoleUser roleUser = new RoleUser();
                roleUser.setUserId(user.getId());
                roleUser.setRoleId(role.getId());
                //角色用户关联表
                if (roleUserService.save(roleUser)){
                    String mIds[] = menuId.split(",");
                    for (int i = 0; i < mIds.length; i++) {
                        RoleMenu roleMenu = new RoleMenu();
                        roleMenu.setMenuId(Long.parseLong(mIds[i]));
                        roleMenu.setRoleId(role.getId());//角色菜单关联表
                        if (!roleMenuService.save(roleMenu)) {
                            throw new CommonException("保存角色失败");
                        }
                    }
                }
            }
        }
        return ResponseUtil.getSuccessMap();
    }


}
